<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/7/12
 * Time: 14:55
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\Passbook;
use app\common\model\Practice;
use app\common\model\Volunteer;
use app\common\model\PracticeLog;
use app\common\model\ExchangeLog;
use app\common\model\User;
use app\common\model\Config;
use app\common\model\Order;
use app\common\model\OrderLog;
use app\common\model\OrderPlace;
use app\common\model\OrderPrice;
use app\vehicle\logic\TplLogic;
use app\vehicle\logic\tokenLogic;
class PassbookLogic extends BaseLogic
{
    private $PassbookModel;

    public function __construct()
    {
        parent::__construct();
        $this->PassbookModel = new Passbook();
    }

    public function getExchange($tempWhere, $nowPage){
        $where = [];
        $tempWhere['user_id'] && $where[] = ['el.user_id','=',$tempWhere['user_id']];

        if($_SESSION['xm_travel']['admin_info']['site_id']){
            $w[] = ['site_id','=',$_SESSION['xm_travel']['admin_info']['site_id']];
        }else if($_SESSION['xm_travel']['admin_info']['oid']){
            $w[] = ['oid','=',$_SESSION['xm_travel']['admin_info']['oid']];
        }
        $w[] = ['is_after','=',1];
        $all_id = implode(',',Volunteer::where($w)->column('user_id'));
        $where[] = ['el.user_id','in',$all_id];

        $count = ExchangeLog::alias('as el')
            ->join('exchange e','e.id = el.exchange_id')
            ->where($where)
            ->count();
        if ($count > 0){
            $list = ExchangeLog::alias('as el')
                ->join('exchange e','e.id = el.exchange_id')
                ->join('user u','u.id = el.user_id')
                ->field('el.*,e.title,e.integral,u.nick_name')
                ->where($where)
                ->page($nowPage, $tempWhere['limit'])
                ->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function getList($tempWhere, $nowPage){
        $where = [];
        if($_SESSION['xm_travel']['admin_info']['site_id']){
            $where[] = ['site_id','=',$_SESSION['xm_travel']['admin_info']['site_id']];
        }else if($_SESSION['xm_travel']['admin_info']['oid']){
            $where[] = ['oid','=',$_SESSION['xm_travel']['admin_info']['oid']];
        }
        $tempWhere['user_id'] && $where[] = ['uid','=',$tempWhere['user_id']];
        $count = $this->PassbookModel->where($where)->count();
        if ($count > 0){
            $list = $this->PassbookModel->with(['gettitle','user'])->where($where)->page($nowPage, $tempWhere['limit'])->select();
            foreach($list as $k=>$v){
                $list[$k]['sign_time'] = date('Y-m-d H:i:s',$v['sign_time']);
                $list[$k]['retreat_time'] = date('Y-m-d H:i:s',$v['retreat_time']);
                $v['s_time'] = $v['sign_time'];
                $v['e_time'] = $v['retreat_time'];
                $times = 0;
                $info = Practice::where('id','eq',$v['aid'])->find();
                if($v['e_time']>0 && $v['e_time']>$v['s_time']){
                    $e_time = $v['e_time'];
                    if($v['e_time']>$info['activity_end_time']){
                        $e_time = $info['activity_end_time'];
                    }
                    $s_time = $v['s_time'];
                    if($v['s_time']<$info['activity_use_time']){
                        $s_time = $info['activity_use_time'];
                    }
                    $times = round(($e_time-$s_time)/3600);
                }
                $list[$k]['times'] = $times>0?$times:0;
                if ($v['type'] == 1) {
                    $duration = Config::where('key', 'eq', 'duration1')->value('value') ?? 1;
                } else {
                    $duration = Config::where('key', 'eq', 'duration2')->value('value') ?? 1;
                }
                if($times>0) {
                    $list[$k]['integral'] = $times * $duration;
                }
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        $data['sign_time'] && $data['sign_time'] = strtotime($data['sign_time']);
        $data['retreat_time'] && $data['retreat_time'] = strtotime($data['retreat_time']);
        switch ($act){
            case 'add':
                $data['create_time'] = time();
                if($data['aid']){
                    $check = PracticeLog::where('user_id','=',$data['uid'])->where('aid','=',$data['aid'])->count();
                    if($check){
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '该用户已报名');
                    }
                    $info = Practice::where('id','=',$data['aid'])->find();
                    if($data['type']){
                        $num = PracticeLog::where('aid','eq',$data['aid'])->where('volunteer','eq',1)->count();
                        if($num>=$info['number']){
                            return '志愿者人数已满';
                        }
                    }else{
                        $num = PracticeLog::where('aid','eq',$data['aid'])->where('volunteer','eq',0)->count();
                        if($num>=$info['numbers']){
                            return '群众人数已满';
                        }
                    }
                    $user = User::where('id','=',$data['uid'])->find();
                    $insert['user_id'] = $data['uid'];
                    $insert['form_id'] = '';
                    $insert['sex'] = '';
                    $insert['site_id'] = $info['site_id'];
                    $insert['oid'] = $info['oid'];
                    $insert['aid'] = $data['aid'];
                    $insert['realname'] = $user['nick_name'];
                    $insert['mobile'] = $user['mobile'];
                    $insert['volunteer'] = $data['type'];
                    $insert['create_time'] = time();
                    $insert['s_time'] =$data['sign_time'];
                    $insert['e_time'] =  $data['retreat_time'];
                    $pid = PracticeLog::insertGetId($insert);
                    $data['pid'] = $pid;
                    $data['site_id'] = $info['site_id'];
                    $data['oid'] = $info['oid'];
                }

                $res = $this->PassbookModel->allowField(true)->save($data);
                break;
            case 'edit':

                $res = $this->PassbookModel->allowField(true)->save($data, $data['id']);
                break;
            case 'sub_info':
                $res = $this->PassbookModel->allowField(true)->save($data, $data['id']);
                break;
            case 'del':
                $Practice = $this->PassbookModel->get($data['id']);
                $res = $Practice->delete();
                break;
            case 'dels':
                $res = Volunteer::where('id','=',$data['id'])->delete();
                break;
            case 'batch':
                return ajaxArr(ErrorCode::$OPERATIONERROR, $data['number']);
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
}